package com.mj.factory.totalSchuedule;

import org.apache.commons.collections4.map.HashedMap;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * Created by Administrator on 2017/3/16.
 */
@Controller
@RequestMapping("mjfactory/schedule")
public class TotalScheduleController {
    private static Logger logger = Logger.getLogger(TotalScheduleController.class);
    @Autowired
    TotalScheduleService totalScheduleService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @PostMapping(value = "queryAll")
    @ResponseBody
    public Map<String, Object> findAll(Integer page, @RequestParam(value = "rows") Integer size) {
        Map<String, Object> map = new HashedMap();
        Page<TotalSchedule> list = totalScheduleService.findAll(new PageRequest(page - 1, size));
        int total = totalScheduleService.findAll().size();
        map.put("total", total);
        map.put("rows", list.getContent());
        return map;
    }

    @PostMapping(value = "add")
    @ResponseBody
    public Map<String, Object> add(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "test") String test) {
        Map<String, Object> map = new HashedMap();
//        try {
//            if (null != file) {
//                technologicalProcessController.importExcel(file);
//                map.put("msg", "success");
//            } else {
//                map.put("msg", "fail");
//            }
//        } catch (Exception e) {
//            map.put("msg", "fail");
//        }
        return map;
    }

    @RequestMapping(value = "getProcessEstimate", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Map<String, Object> getJsonDate() {
        Map<String, Object> map = new HashMap();
        String sqlData = "SELECT " +
                "  round(SUM(sawing_machine), 2) AS '锯床', " +
                "  round(SUM(lathe), 2) AS '车床', " +
                "  round(SUM(milling_machine), 2) AS '铣床', " +
                "  round(SUM(cnc), 2) AS 'CNC', " +
                "  round(SUM(drilling_machine), 2) AS '钻床', " +
                "  round(SUM(tapping), 2) AS '攻牙', " +
                "  round(SUM(line_cutting), 2) AS '线切割', " +
                "  round(SUM(welding), 2) AS '焊接', " +
                "  round(SUM(grinding), 2) AS '打磨' " +
                " FROM " +
                "  mj_totalschedule ";
        List<Map<String, Object>> dataList = null;
        List<String> categories = new ArrayList<>();
        List<String> data = new ArrayList<>();
        try {
            dataList = jdbcTemplate.queryForList(sqlData);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        if (null != dataList && dataList.size() > 0) {
            Map<String, Object> m = dataList.get(0);
            for (Map.Entry<String, Object> entry : m.entrySet()) {
                categories.add(entry.getKey());
                data.add(String.valueOf(entry.getValue()));
            }
        }
        map.put("categories", categories);
        map.put("data", data);
        return map;
    }
}

